/*
 * Copyright (C) Michael Opitz
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA
 */
#ifndef RECEIVER_H
#define RECEIVER_H

#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/thread.hpp>

#include "parser.h"
#include "worker_thread.h"
#include "network.h"
#include "i_socket.h"
#include "parse_exception.h"
#include "notifier.h"

#include "socket/socket.h"
#include "socket/lost_connection_exception.h"

#include <iostream>

using namespace std;

/** 
 * \brief This namespaces includes all nertwork related stuff.
 * \author Michael Opitz
 */
namespace network {
	/** 
	 * \brief This class receives strings from a socket, parses them and
	 * inserts messages into the message_queue.
	 */
	class receiver : public network::worker_thread,
			 public network::notifier {
	public:
		/** 
		 * \brief receiver is the constructor for this class. Don't call
		 * this contructor directly, use create instead.
		 * 
		 * \param sock is the socket from which messages should be
		 * received
		 * \param queue is the message_queue into which messages should
		 * be inserted
		 * \param pars is the parses, with which strings should be
		 * parsed
		 */
		receiver(boost::shared_ptr<network::i_socket> sock, 
			 boost::shared_ptr<network::message_queue> queue,
			 boost::shared_ptr<network::parser> pars)
		: _sock(sock), _queue(queue), _pars(pars) {}
		virtual ~receiver();

		/** 
		 * \brief run runs the receiver-thread. Don't call this method
		 * directly, use start_thread() instead.
		 * \see network::worker_thread
		 */
		virtual void run();
		/** 
		 * \brief get_socket returns the socket
		 * 
		 * \return a shared_ptr to a socket is returned.
		 */
		boost::shared_ptr<const network::i_socket> get_socket() const
		{
			return _sock;
		}

		/** 
		 * \brief create creates a new receiver-object.
		 * 
		 * \see receiver
		 * \return a shared_ptr to a receiver-object is returned.
		 */
		static boost::shared_ptr<network::receiver> create(
				boost::shared_ptr<network::i_socket> sock,
				boost::shared_ptr<network::message_queue> queue,
				boost::shared_ptr<network::parser> pars)
		{
			boost::shared_ptr<network::receiver> rc(new
				network::receiver(sock, queue, pars));
			rc->_this = rc;

			return rc;
		}
	private:
		void send_delete();
		boost::shared_ptr<network::i_socket> _sock;
		boost::shared_ptr<network::message_queue> _queue;
		boost::shared_ptr<network::parser> _pars;
		
		boost::weak_ptr<network::receiver> _this;
	};
}

#endif

